package org.linlinjava.litemall.admin.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.beans.pojo.convert.BeanConvert;
import org.linlinjava.litemall.admin.beans.vo.CollectVo;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallCollect;
import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.service.LitemallCollectService;
import org.linlinjava.litemall.db.service.LitemallGoodsService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理员收集控制器
 *
 * @author yliyun
 * @date 2022/09/14
 */
@RestController
@RequestMapping("/admin/collect")
@Validated
public class AdminCollectController {

    /**
     * 收集服务
     */
    @Autowired
    private LitemallCollectService collectService;
    /**
     * 商品服务
     */
    @Autowired
    private LitemallGoodsService goodsService;
    /**
     * 用户服务
     */
    @Autowired
    private LitemallUserService userService;

    /**
     * 列表
     *
     * @param userId  用户id
     * @param valueId id值
     * @param page    页面
     * @param limit   限制
     * @param sort    排序
     * @param order   订单
     * @return {@link Object}
     */
    @GetMapping("/list")
    @LogAnno
    public Object list(String userId, String valueId,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<LitemallCollect> collectList = collectService.querySelective(userId, valueId, page, limit, sort, order);
        List<CollectVo> rtn =  collectList.stream().map(collect->{
            LitemallGoods goods = goodsService.findById(collect.getGoodsId());
            LitemallUser user = userService.findById(collect.getUserId());
            return BeanConvert.toCollectVo(collect,goods != null?goods.getName():null,user!= null?user.getUsername():null);
        }).collect(Collectors.toList());
        return ResponseUtil.okList(rtn);
    }
}
